package com.xiaofan.config;

import com.xiaofan.Interceptor.DemoInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 晓帆
 * @version 1.0
 **/
@EnableWebMvc//开启mvc注解驱动
@ComponentScan("com.xiaofan.controller")
public class SpringMVCConfig implements WebMvcConfigurer {//实现接口提供方法实现mvc标签功能

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //指定拦截器实例以及拦截范围
        registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/hello/**");
    }
    //配置静态资源文件映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //配置url地址以及静态资源位置
        registry.addResourceHandler("/imgs/**").addResourceLocations("/imgs/");
    }
//配置自定义的转换器
    @Override
    public void addFormatters(FormatterRegistry registry) {
//        registry.addConverter();
    }
//注册参数验证
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        return localValidatorFactoryBean;
    }

}
